package com.rjhch.tonglink.client.base;

import com.tongtech.tlq.base.*;

import java.util.*;
import java.io.*;

public class RetransferMsg  extends Thread{
	private String qcuName;
	private int thrdId;
	
	public RetransferMsg(String qcuName,int thrdId){
		this.qcuName=qcuName;
		this.thrdId=thrdId;
	}
	
  public int retransfer(String QcuName,int k){
    TlqConnContext conncontext = new TlqConnContext();
    conncontext.BrokerId = -1;
    conncontext.HostName = "127.0.0.1";
    conncontext.ListenPort = 10261;
    conncontext.ConnId="Conn"+k;
    conncontext.FilesDir="D:\\TLQ72Trunk1\\samples\\nodes\\TCli\\files";
    conncontext.LogFileName="D:\\TLQ72Trunk1\\samples\\nodes\\TCli\\log\\log.sys";
	
    TlqConnection tlqConnection = null;
    TlqQCU tlqQcu = null;

    try {
      tlqConnection = new TlqConnection(conncontext);
      tlqQcu = tlqConnection.openQCU(QcuName);

      System.out.println("Begin to receive message");
      TlqMessage msgInfo = new TlqMessage();
      TlqMsgOpt msgOpt = new TlqMsgOpt();
      tlqQcu.retransfer(msgInfo, msgOpt);
      dumpMsgInfo(msgInfo, msgOpt);     
    } catch (TlqException tlqEx) {
    	tlqEx.printStackTrace();
      System.out.println("errno is :" + tlqEx.getTlqErrno());
      System.out.println("syserr is :" + tlqEx.getSysErrno());
      System.out.println("reason is :" + tlqEx.getErrorCause());
      return -1;
    }finally{
      try {
        tlqQcu.close();
        tlqConnection.close();
      } catch (TlqException e) {
        // do nothing
        e.printStackTrace();
      }
      
    }
    System.out.println("RetransferMsg retransfer is over....");
    return 0;
  }


  static public void dumpMsgInfo(TlqMessage msgInfo, TlqMsgOpt msgOpt) {
    System.out.println("msgInfo.MsgType= " + (int) msgInfo.MsgType);

    System.out.println("msgInfo.Data = " + new String(msgInfo.getMsgData()));

    System.out
        .println("msgOpt.RemoveFileFlag= " + (byte) msgOpt.RemoveFileFlag);

    /*
     * System.out.println( "msgOpt.CompressFlag= " +
     * String.valueOf(msgOpt.CompressFlag)); System.out.println(
     * "msgOpt.CryptFlag= " + String.valueOf(msgOpt.CryptFlag));
     * System.out.println("msgInfo.Priority= " + (int) msgInfo.Priority);
     */if (msgOpt.QueName != null)
      System.out.println("msgOpt.QueName= " + new String(msgOpt.QueName));
    else
      System.out.println("msgInfo.queName= null");

    System.out.println("msgInfo.Expiry= " + (int) msgInfo.Expiry);
    System.out.println("msgInfo.MsgSize= " + (int) msgInfo.MsgSize);
    System.out.println("msgOpt.ReportType = " + (int) msgOpt.ReportType);
    if (msgOpt.ReportQName != null)
      System.out.println("msgOpt.ReportQName= "
          + new String(msgOpt.ReportQName));
    else
      System.out.println("msgOpt.ReportQName= null");

    System.out.println("msgInfo.Priority= " + (int) msgInfo.Priority);
    System.out.println("msgInfo.OrgMsgLen= " + msgInfo.OrgMsgLen);
    if (msgInfo.SrcNode != null)
      System.out.println("msgInfo.SrcNode= " + new String(msgInfo.SrcNode));
    else
      System.out.println("msgInfo.SrcNode= null");
    System.out.println("msgInfo.UserPropertyNum= "+msgInfo.getPropertyNum());
  }

   public void run(){
   	try{
		echo("--------"+thrdId+"-------");
		retransfer(qcuName,thrdId);	
   	}catch(Exception e){

   		e.printStackTrace();
	}
   }
   
  public static void main(String[] argv) throws Exception {
    String QcuName;

    if (argv.length < 2) {
      System.out.println("--------------请输入参数！--------------\n");
      System.out
          .println("RetransferMsg QcuName ThrNum");
      return;
    } else {
      QcuName = argv[0];
      int thrNum=Integer.parseInt(argv[1]);
      if(thrNum<=0){
	  	echo("ThrNum <0");
		return;
      }
      Thread[] thrds=new Thread[thrNum];
      for(int i=0;i<thrNum;i++){
		thrds[i]=new RetransferMsg(QcuName,i);
      }
      for(int i=0;i<thrNum;i++){
	  	thrds[i].start();
      	}
	for(int i=0;i<thrNum;i++){
		thrds[i].join();
	}
    }
    System.out.println("--------------");
  }

  private static void echo(String str){
  	System.out.println(str);
  }
}
